package com.etc;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;



import java.io.IOException;

/**
 * @author 郭朋朋
 * @Title:
 * @ProjectName
 * @Description:
 */


/**
 * conf配置文件
 * connection连接
 * admin管理者
 */
public class HbaseUtils {
    protected static Configuration conf = null;
    protected static Connection connection = null;
    protected static Admin admin = null;


    /**
     * create 读取数据
     * 读xml的配置   zookeeper地址
     * 创建一个连接
     * 拿到一个DDl操作器：表管理器admin  admin增删改查
     */

    public static void getHbaseConnection() throws IOException {
        try {
            conf = HBaseConfiguration.create();
            conf.set("hbase.zookeeper.quorum", "m1,s2,s3");
            connection = ConnectionFactory.createConnection();
            admin = connection.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 连关闭接
     */
    public static void close() {
        try {
            if (connection != null) {
                connection.close();
            }
            if (admin != null) {

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建表  第一个是表  第二是族 第三是列    根据数组 切分key
     */
    public static void createTable(String nameSpace, String tableName, String faminly, byte[][] splitKeys) {
        try {
            TableName tName = TableName.valueOf(nameSpace, tableName);
//        如果存在  1.禁言 2.删除
            if (admin.tableExists(tName)) {
                admin.disableTable(tName);
                admin.deleteTable(tName);
            } else {
                HTableDescriptor tableDescriptor = new HTableDescriptor(tName);
                HColumnDescriptor colDesc = new HColumnDescriptor(faminly.getBytes());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @description 删除row列族下的某列值
     */
    public static void deleteQualifierValue(String tableName, String rowkey, String family, String qualifier) {
        Table table = null;
        try {
            table = connection.getTable(TableName.valueOf(tableName));
            Delete delete = new Delete(family.getBytes());
            delete.addColumn(family.getBytes(), qualifier.getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (table != null) {
                try {
                    table.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (table != null) {
                        try {
                            table.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }

            /**
             * @desc 删除一行
             */
            public static void deleteRow(String tableName, String rowKey, String family) {

            }

        }

    }
}
